﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Appsters
{
    public partial class planner : Form
    {
        private OleDbConnection dbConn;

        public planner()
        {
            InitializeComponent();

            OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

            csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            csb["data source"] = Application.StartupPath + @"\Jotter.accdb";

            string connStr = csb.ConnectionString;
            dbConn = new OleDbConnection(connStr);

            string docPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string pathMonthNotes = System.IO.Path.Combine(docPath, "Jotter\\Saved Files\\Month Notes");
            string pathNotes = System.IO.Path.Combine(docPath, "Jotter\\Saved Files\\Notes");
            string pathTasks = System.IO.Path.Combine(docPath, "Jotter\\Saved Files\\Tasks");
            System.IO.Directory.CreateDirectory(pathMonthNotes);
            string pinNote = System.IO.Path.Combine(pathMonthNotes, "MonthlyNotes.txt");

            if (!System.IO.File.Exists(pinNote))
            {
                using (var createFile = new System.IO.FileStream(@pinNote, System.IO.FileMode.Create)) ;
            }
            else
            {
                using (var file = new System.IO.FileStream(@pinNote, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Write))
                using (var reader = new System.IO.StreamReader(file))
                {
                    nBox.Text = reader.ReadToEnd();
                }
            }

            calendar.CalendarDimensions = new System.Drawing.Size(2, 2);

            boldDates(pathNotes, pathTasks);
        }
        
        private void boldDates(String notes, String tasks)
        {
            dbConn.Open();

            string qryStr = @"SELECT dateField FROM Notes";

            OleDbCommand cmd = new OleDbCommand(qryStr, dbConn);

            OleDbDataReader reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    string q = reader.GetValue(0).ToString();
                    String[] w = q.Split('/');
                    int dd = Int32.Parse(w[1]);
                    int mm = Int32.Parse(w[0]);
                    int yyyy = Int32.Parse(w[2].Substring(0, 4));
                    calendar.AddBoldedDate(new DateTime(yyyy, mm, dd));
                }
            }catch(Exception e)
            {

            }

            qryStr = @"SELECT theDate FROM Task";

            cmd = new OleDbCommand(qryStr, dbConn);

            reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    string q = (string)reader.GetValue(0);
                    String[] w = q.Split('-');
                    int dd = Int32.Parse(w[2]);
                    int mm = Int32.Parse(w[1]);
                    int yyyy = Int32.Parse(w[0]);
                    calendar.AddBoldedDate(new DateTime(yyyy, mm, dd));
                }
            } catch (Exception v)
            {

            }
            
            dbConn.Close();

        }
        
        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void backToProfile_Click(object sender, EventArgs e)
        {
            backToProfile.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            profView p = new profView();
            p.Show();
            this.Hide();
        }

        private void toNotes_Click_1(object sender, EventArgs e)
        {
            toNotes.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            notes n = new notes();
            n.Show();
            this.Hide();
        }

        private void dailyView_Click(object sender, EventArgs e)
        {
            dailyView.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            DailyView d = new DailyView();
            d.Show();
            this.Hide();
        }

        private void toYearly_Click(object sender, EventArgs e)
        {
            toYearly.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            yearView yv = new yearView();
            yv.ShowDialog();
            this.Close();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            String[] fD = calendar.SelectionRange.Start.ToShortDateString().Split('/');
            int dd = Int32.Parse(fD[0]);
            int mm = Int32.Parse(fD[1]);
            int yy = Int32.Parse(fD[2]);
            int counterNotes = 0;
            int counterTasks = 0;

            dbConn.Open();

            string qryStr = @"SELECT dateField FROM Notes";

            OleDbCommand cmd = new OleDbCommand(qryStr, dbConn);

            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                string q = reader.GetValue(0).ToString();
                String[] w = q.Split('/');
                if ((dd == Int32.Parse(w[0])) & (mm == Int32.Parse(w[1])) & (yy == Int32.Parse(w[2].Substring(0, 4))))
                {
                    counterNotes++;
                }
            }

            qryStr = @"SELECT theDate FROM Task";

            cmd = new OleDbCommand(qryStr, dbConn);

            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                String q = (string)reader.GetValue(0);
                String[] w = q.Split('-');
                if ((dd == Int32.Parse(w[2])) & (mm == Int32.Parse(w[1])) & (yy == Int32.Parse(w[0])))
                {
                    counterTasks++;
                }
            }
            dbConn.Close();

            ToolTip t = new ToolTip();
            t.SetToolTip(calendar, "" + counterNotes + " Notes. " + counterTasks + " Tasks.");
        }

        private void nBox_TextChanged(object sender, EventArgs e)
        {
            string docPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string monthNotesFolder = System.IO.Path.Combine(docPath, "Jotter\\Saved Files\\Month Notes");
            string path = System.IO.Path.Combine(monthNotesFolder, "MonthlyNotes.txt");

            using (var fileStream = new System.IO.FileStream(@path, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read))
            using (var streamWriter = new System.IO.StreamWriter(fileStream))
            {
                streamWriter.WriteLine(nBox.Text);
            } 
        }
    }
}
